//
//  YBEarnViewController.m
//  Nectar
//
//  Created by famer on 2023/11/10.
//  Copyright © 2023 asance. All rights reserved.
//

#import "YBEarnViewController+LayoutConstraints.h"
#import "YBTransferInEarnView.h"
#import "YBNavigationBar.h"
#import "YBReceiveViewController.h"
#import "YBInviteViewController.h"
#import "HomeCoinManager.h"
#import "YBInviteEarnView.h"
#import "RevDataGetRechargeIncomeConfig.h"
#import "RevDataGetTransferInEarn.h"
#import "RevDataGetInviteFriendEarn.h"
#import "YBEarnTransferViewController.h"
#import "YBEarnDetailInviteEarnViewController.h"
#import "YBWithdrawTransferEarnViewController.h"
#import "YBWithdrawInviteEarnViewController.h"

@interface YBEarnViewController ()
@property (strong, nonatomic) NSMutableArray *configArray;
@end

@implementation YBEarnViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = kWhiteColor;
    
    [self.view addSubview:self.navigationBar];
    
    [self.view addSubview:self.myScrollView];
    [self.myScrollView addSubview:self.todayEarnBronzeView];
    [self.myScrollView addSubview:self.todayEarnGoldView];
    [self.myScrollView addSubview:self.todayEarnPlatinumView];
    [self.myScrollView addSubview:self.todayEarnDiamondView];
    
    [self.view addSubview:self.menuContentView];
    [self.menuContentView addSubview:self.withdrawImageView];
    [self.menuContentView addSubview:self.withdrawLabel];
    [self.menuContentView addSubview:self.withdrawButton];
    [self.menuContentView addSubview:self.inviteImageView];
    [self.menuContentView addSubview:self.inviteLabel];
    [self.menuContentView addSubview:self.inviteButton];
    [self.menuContentView addSubview:self.receiveImageView];
    [self.menuContentView addSubview:self.receiveLabel];
    [self.menuContentView addSubview:self.receiveButton];
    
    [self.view addSubview:self.inviteEarnView];
    
    [self setLayoutConstraints];
    
    [self updateUI];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    [self.navigationBar resetBarItemState];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    [self getRechargeIncomeConfig];
}
#pragma mark - Private Method
- (void)updateUI{    
    self.menuContentView.layer.cornerRadius = 6;
    self.menuContentView.layer.borderColor = [UIColor hexColor:@"F6F5F6"].CGColor;
    self.menuContentView.layer.borderWidth = 1;
}

- (void)toReceiveViewController{
    YBReceiveViewController *viewcontroller = [[YBReceiveViewController alloc] init];
    viewcontroller.walletAddress = [HomeCoinManager shareInstance].globalAddress;
    viewcontroller.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:viewcontroller animated:YES];
}

- (void)toInviteViewController{
    YBInviteViewController *viewcontroller = [[YBInviteViewController alloc] init];
    viewcontroller.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:viewcontroller animated:YES];
}

- (void)toWithdrawTransferEarnViewController{
    YBWithdrawTransferEarnViewController *viewcontroller = [[YBWithdrawTransferEarnViewController alloc] init];
    viewcontroller.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:viewcontroller animated:YES];
}

- (void)toWithdrawInviteEarnViewController{
    YBWithdrawInviteEarnViewController *viewcontroller = [[YBWithdrawInviteEarnViewController alloc] init];
    viewcontroller.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:viewcontroller animated:YES];
}

- (void)toEarnDetailInviteEarnViewController{
    YBEarnDetailInviteEarnViewController *viewcontroller = [[YBEarnDetailInviteEarnViewController alloc] init];
    viewcontroller.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:viewcontroller animated:YES];
}

- (void)toEarnTransferViewController{
    YBEarnTransferViewController *viewcontroller = [[YBEarnTransferViewController alloc] init];
    viewcontroller.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:viewcontroller animated:YES];
}


- (void)toMyWalletViewController{
    //    YBMyWalletsViewController *viewcontroller = [[YBMyWalletsViewController alloc] init];
    //    viewcontroller.hidesBottomBarWhenPushed = YES;
    //    [self.navigationController pushViewController:viewcontroller animated:YES];
}

- (void)toManageWalletViewController{
    //    YBManageWalletViewController *viewcontroller = [[YBManageWalletViewController alloc] init];
    //    viewcontroller.coinType = [HomeCoinManager shareInstance].globalCoinType;
    //    viewcontroller.hidesBottomBarWhenPushed = YES;
    //    [self.navigationController pushViewController:viewcontroller animated:YES];
}

- (void)getRechargeIncomeConfig{
    [SVProgressHUD show];
    self.view.userInteractionEnabled = NO;
    NSMutableDictionary *params = [NSDictionary baseReqParams];
    NSString *urlString = [NSString stringWithFormat:@"%@",PathGetRechargeIncomeConfig];
    
    [[YBHttpRequestManager shareInstance] Post:urlString params:params success:^(NSObject *resultObject) {
        NSLog(@"getRechargeIncomeConfig: %@",resultObject);
        
        self.view.userInteractionEnabled = YES;
        [SVProgressHUD dismiss];
        
        //判断网络是否返回错误信息
        YBResponseModel *obj = [YBResponseModel initWithResponseObject:(NSDictionary *)resultObject];
        if([obj success]){
            NSArray *configArray = [RevDataGetRechargeIncomeConfig mj_objectArrayWithKeyValuesArray:obj.data];
            [self.configArray removeAllObjects];
            [self.configArray addObjectsFromArray:configArray];
            
            //获取转入收益总览
            [self getTransferInEarn];
            
            //获取邀请好友收益总览
            [self getInviteFriendEarn];
        }
        
    } failure:^(NSError *error) {
        self.view.userInteractionEnabled = YES;
        YBErrorModel *yerror = [YBErrorModel ErrorObjForNSError:error];
    }];
}

- (void)getTransferInEarn{
    self.view.userInteractionEnabled = NO;
    NSMutableDictionary *params = [NSDictionary baseReqParams];
    [params setObject:[HomeCoinManager shareInstance].globalCoinType forKey:@"coinType"];
    NSString *urlString = [NSString stringWithFormat:@"%@",PathGetTransferInEarn];
    
    [[YBHttpRequestManager shareInstance] Post:urlString params:params success:^(NSObject *resultObject) {
        NSLog(@"getTransferInEarn: %@",resultObject);
        
        self.view.userInteractionEnabled = YES;
        [SVProgressHUD dismiss];
        
        //判断网络是否返回错误信息
        YBResponseModel *obj = [YBResponseModel initWithResponseObject:(NSDictionary *)resultObject];
        if([obj success]){
            RevDataGetTransferInEarn *result = [RevDataGetTransferInEarn mj_objectWithKeyValues:obj.data];
            
            //更新ui
            [self.todayEarnBronzeView setConfigData:[self getConfigData:LEVEL_BRONZE] realData:result];
            [self.todayEarnGoldView setConfigData:[self getConfigData:LEVEL_GOLD] realData:result];
            [self.todayEarnPlatinumView setConfigData:[self getConfigData:LEVEL_PLATINUM] realData:result];
            [self.todayEarnDiamondView setConfigData:[self getConfigData:LEVEL_DIAMOND] realData:result];
        }
        
    } failure:^(NSError *error) {
        self.view.userInteractionEnabled = YES;
        YBErrorModel *yerror = [YBErrorModel ErrorObjForNSError:error];
    }];
}

- (void)getInviteFriendEarn{
    [SVProgressHUD show];
    self.view.userInteractionEnabled = NO;
    NSMutableDictionary *params = [NSDictionary baseReqParams];
    [params setObject:[HomeCoinManager shareInstance].globalCoinType forKey:@"coinType"];
    NSString *urlString = [NSString stringWithFormat:@"%@",PathGetInviteFriendEarn];
    
    [[YBHttpRequestManager shareInstance] Post:urlString params:params success:^(NSObject *resultObject) {
        NSLog(@"getInviteFriendEarn: %@",resultObject);
        
        self.view.userInteractionEnabled = YES;
        [SVProgressHUD dismiss];
        
        //判断网络是否返回错误信息
        YBResponseModel *obj = [YBResponseModel initWithResponseObject:(NSDictionary *)resultObject];
        if([obj success]){
            RevDataGetInviteFriendEarn *result = [RevDataGetInviteFriendEarn mj_objectWithKeyValues:obj.data];
            [self.inviteEarnView setRealData:result];
        }
        
    } failure:^(NSError *error) {
        self.view.userInteractionEnabled = YES;
        YBErrorModel *yerror = [YBErrorModel ErrorObjForNSError:error];
    }];
}

- (RevDataGetRechargeIncomeConfig *)getConfigData:(NSString *)level{
    for(int i=0;i<self.configArray.count;i++){
        RevDataGetRechargeIncomeConfig *item = self.configArray[i];
        if([item.level isEqualToString:level]){
            return item;
        }
    }
    return nil;
}

#pragma mark Getter Setter
YBViewGetter(menuContentView, _menuContentView, [UIColor hexColor:@"FFFFFF"])
YBImageViewGetter(withdrawImageView, _withdrawImageView, [UIImage imageNamed:@"icon_earn_withdraw"])
YBLabelGetter(withdrawLabel, _withdrawLabel, [UIFont systemFontOfSize:[NSNumber adaptToHeight:12]], @"Withdraw", [UIColor hexColor:@"1C1C1C"], NSTextAlignmentCenter);
YBImageViewGetter(inviteImageView, _inviteImageView, [UIImage imageNamed:@"icon_earn_invite"])
YBLabelGetter(inviteLabel, _inviteLabel, [UIFont systemFontOfSize:[NSNumber adaptToHeight:12]], @"Invite", [UIColor hexColor:@"1C1C1C"], NSTextAlignmentCenter);
YBImageViewGetter(receiveImageView, _receiveImageView, [UIImage imageNamed:@"icon_receive"])
YBLabelGetter(receiveLabel, _receiveLabel, [UIFont systemFontOfSize:[NSNumber adaptToHeight:12]], @"Receive", [UIColor hexColor:@"1C1C1C"], NSTextAlignmentCenter);


- (YBInviteEarnView *)inviteEarnView{
    if(!_inviteEarnView){
        _inviteEarnView = [[YBInviteEarnView alloc] initWithFrame:CGRectZero];
        __weak __typeof(&*self)weakSelf = self;
        [_inviteEarnView setWithdrawAction:^{
            __strong __typeof(&*weakSelf)strongSelf = weakSelf;
            if(!strongSelf) return;
            
            [strongSelf toWithdrawInviteEarnViewController];
        }];
        [_inviteEarnView setToDetailAction:^{
            __strong __typeof(&*weakSelf)strongSelf = weakSelf;
            if(!strongSelf) return;
            
            [strongSelf toEarnDetailInviteEarnViewController];
        }];
    }
    return _inviteEarnView;
}

- (YBNavigationBar *)navigationBar{
    if(!_navigationBar){
        _navigationBar = [[YBNavigationBar alloc] initWithFrame:CGRectZero];
        _navigationBar.backgroundColor = kWhiteColor;
        [_navigationBar setTitle:@"Earn" titleColor:kThemeBlackColor];
        [_navigationBar setLeftBarImage:@"icon_nav_menu" forState:UIControlStateNormal];
        __weak __typeof(&*self)weakSelf = self;
        _navigationBar.onLeftBarButtonBlock = ^(){
            __strong __typeof(&*weakSelf)strongSelf = weakSelf;
            if(!strongSelf) return;
            
        };
    }
    return _navigationBar;
}

- (YBTransferInEarnView *)todayEarnBronzeView{
    if(!_todayEarnBronzeView){
        _todayEarnBronzeView = [[YBTransferInEarnView alloc] initWithFrame:CGRectZero];
        _todayEarnBronzeView.levelType = LEVEL_BRONZE;
        __weak __typeof(&*self)weakSelf = self;
        _todayEarnBronzeView.actionBlock = ^{
            __strong __typeof(&*weakSelf)strongSelf = weakSelf;
            if(!strongSelf) return;
            [strongSelf toEarnTransferViewController];
        };
    }
    return _todayEarnBronzeView;
}

- (YBTransferInEarnView *)todayEarnGoldView{
    if(!_todayEarnGoldView){
        _todayEarnGoldView = [[YBTransferInEarnView alloc] initWithFrame:CGRectZero];
        _todayEarnGoldView.levelType = LEVEL_GOLD;
        __weak __typeof(&*self)weakSelf = self;
        _todayEarnGoldView.actionBlock = ^{
            __strong __typeof(&*weakSelf)strongSelf = weakSelf;
            if(!strongSelf) return;
            [strongSelf toEarnTransferViewController];
        };
    }
    return _todayEarnGoldView;
}

- (YBTransferInEarnView *)todayEarnPlatinumView{
    if(!_todayEarnPlatinumView){
        _todayEarnPlatinumView = [[YBTransferInEarnView alloc] initWithFrame:CGRectZero];
        _todayEarnPlatinumView.levelType = LEVEL_PLATINUM;
        __weak __typeof(&*self)weakSelf = self;
        _todayEarnPlatinumView.actionBlock = ^{
            __strong __typeof(&*weakSelf)strongSelf = weakSelf;
            if(!strongSelf) return;
            [strongSelf toEarnTransferViewController];
        };
    }
    return _todayEarnPlatinumView;
}

- (YBTransferInEarnView *)todayEarnDiamondView{
    if(!_todayEarnDiamondView){
        _todayEarnDiamondView = [[YBTransferInEarnView alloc] initWithFrame:CGRectZero];
        _todayEarnDiamondView.levelType = LEVEL_DIAMOND;
        __weak __typeof(&*self)weakSelf = self;
        _todayEarnDiamondView.actionBlock = ^{
            __strong __typeof(&*weakSelf)strongSelf = weakSelf;
            if(!strongSelf) return;
            [strongSelf toEarnTransferViewController];
        };
    }
    return _todayEarnDiamondView;
}

- (UIScrollView *)myScrollView{
    if(!_myScrollView){
        _myScrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
        _myScrollView.backgroundColor = [UIColor whiteColor];
        _myScrollView.pagingEnabled = YES;
        _myScrollView.clipsToBounds = NO;
        _myScrollView.showsVerticalScrollIndicator = NO;
        _myScrollView.showsHorizontalScrollIndicator = NO;
        _myScrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0); // 设置左右两侧留白
        
        if (@available(iOS 11.0, *)) {
            _myScrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
    }
    return _myScrollView;
}

- (UIButton *)withdrawButton{
    if(!_withdrawButton){
        _withdrawButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _withdrawButton.backgroundColor = kClearColor;
        [_withdrawButton setTitle:@"" forState:UIControlStateNormal];
        [_withdrawButton setTitle:@"" forState:UIControlStateSelected];
        [_withdrawButton addTarget:self action:@selector(toWithdrawTransferEarnViewController) forControlEvents:UIControlEventTouchUpInside];
    }
    return  _withdrawButton;
}

- (UIButton *)inviteButton{
    if(!_inviteButton){
        _inviteButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _inviteButton.backgroundColor = kClearColor;
        [_inviteButton setTitle:@"" forState:UIControlStateNormal];
        [_inviteButton setTitle:@"" forState:UIControlStateSelected];
        [_inviteButton addTarget:self action:@selector(toInviteViewController) forControlEvents:UIControlEventTouchUpInside];
    }
    return  _inviteButton;
}

- (UIButton *)receiveButton{
    if(!_receiveButton){
        _receiveButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _receiveButton.backgroundColor = kClearColor;
        [_receiveButton setTitle:@"" forState:UIControlStateNormal];
        [_receiveButton setTitle:@"" forState:UIControlStateSelected];
        [_receiveButton addTarget:self action:@selector(toReceiveViewController) forControlEvents:UIControlEventTouchUpInside];
    }
    return  _receiveButton;
}

- (NSMutableArray *)configArray{
    if(!_configArray){
        _configArray = [[NSMutableArray alloc]init];
    }
    return _configArray;;
}

@end
